#!/bin/bash
function cs_info () {
  local extVar=$1[@]
  local extents=(${!extVar})

  local fnamesVar=$2[@]
  local fileNames=(${!fnamesVar})

  local excludeNameVar=$3[@]
  local excludeNames=(${!excludeNameVar})

  echo "Index file exts: ${extents[@]}"
  echo "Index file names: ${fileNames[@]}"
  echo "Exclude dirs: ${excludeNames[@]}"
}

function build_cs_file_list() {

  # 待搜索根目录
  local root="$1"

  # 要搜索的文件扩展名
  local extVar=$2[@]
  local extents=(${!extVar})

  # 要搜索的文件全名
  local fnamesVar=$3[@]
  local fileNames=(${!fnamesVar})

  local excludeNameVar=$4[@]
  local excludeNames=(${!excludeNameVar})

  # 忽略掉excludeNames
  local find_opts=""
  for e in "${excludeNames[@]}"
  do
    find_opts+="-name $e -type d -prune -o "
  done

  # 组织要find的文件扩展名参数(-name ... -o -name ... ...)
  if [ ${#extents[@]} -gt 0 ]
  then
    for ext in "${extents[@]}"
    do
      find_opts+="-name \"*.${ext}\" -type f -print -o "
    done
  else
    find_opts+="-type f -print -o "
  fi

  # 文件全名查找
  for fname in "${fileNames[@]}"
  do
    find_opts+="-name \"${fname}\" -type f -print -o "
  done
  
  # 去掉最后的-o
  find_opts=${find_opts%-*}
  
  # echo ${find_opts}
  
  csFile="./cscope.files"
  echo "Build ${csFile} ..."
  # 这里直接调用find会出错，原因未知:(
  bash -c "find ${root} ${find_opts}" > "${csFile}"
}
